section	.bss
buf	resq	1
oct	resq	1

section	.data
global _start
_start:
	call	getchar

	cmp	rax,-1
	je	.done
	
	cmp	rax,' '
	je	_start

	cmp	rax,0x09
	je	_start

	cmp	rax,0x0A
	je	_start
	
	cmp	rax,'#'
	je	skip_comment
	
	cmp	rax,':'
	je	read_label
	
	call	hex_digit
	shl	rax,4
	mov	[oct],rax
	call	getchar
	call	hex_digit
	or	[oct],rax

	mov	rax,[oct]
	call	putchar
	
	jmp	_start

.done:
	mov	rax,60
	mov	rdi,0
	syscall

skip_comment:
	call	getchar
	
	cmp	rax,-1
	je	die
	
	cmp	rax,0x0A
	je	_start

	jmp	skip_comment

read_label:
	
	jmp	skip_comment

hex_digit:
	cmp	rax,'0'
	jl	.next1
	
	cmp	rax,'9'
	jg	.next1
	
	sub	rax,'0'
	ret

.next1:
	cmp	rax,'a'
	jl	.next2
	
	cmp	rax,'f'
	jg	.next2
	
	sub	rax,('a'-10)
	ret

.next2:
	cmp	rax,'A'
	jl	die
	
	cmp	rax,'F'
	jg	die
	
	sub	rax,('A'-10)
	ret

die
	mov	rax,60
	mov	rdi,1
	syscall

getchar:
	mov	rax,0x00
	mov	rdi,0		;stdin fd
	lea	rsi,[buf]
	mov	rdx,1		;count
	syscall
	
	cmp	rax,1
	jne	.getchar_fail

	mov	rax,0
	mov	al,[buf]
	ret

.getchar_fail:
	mov	rax,-1
	ret

putchar:
	mov	[buf],al

	mov	rax,0x01
	mov	rdi,1		;stdout fd
	lea	rsi,[buf]
	mov	rdx,1
	syscall

	ret


